// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/lbank/api_util/auth_key.h"
#include "coin2/exchange/lbank/api_util/rate_tracker.h"
#include "coin2/exchange/lbank/symbology/product.h"
#include "coin2/exchange/order/lbank.pb.h"

namespace coin2::exchange::lbank::api_order {

class LbankNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using RestParams = coin2::exchange::order::LbankRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using ActionType = coin2::exchange::base::order::NativePrivateClient::ActionType;
  using LbankRateTracker = coin2::exchange::lbank::api_util::LbankRateTracker;
  using LbankProduct = coin2::exchange::lbank::symbology::LbankProduct;

 public:
  explicit LbankNativePrivateClient(const MarketExchangeApi& mea, const NativeClientConfig& config);

  std::shared_ptr<AsioHttpContext> QueryAccountBalance();
  std::shared_ptr<AsioHttpContext> QueryOpenOrders(const RestParams& params, int64_t current_page);
  std::shared_ptr<AsioHttpContext> QueryHistoryOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryFills(const RestParams& params);

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);
  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> GenListenKey();
  std::shared_ptr<AsioHttpContext> RefreshListenKey();
  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&) override;

  void Prepare();
  std::string DebugString() const;

 private:
  int64_t start_time_;
  std::string subscribe_key_;
  std::unordered_map<std::string, int32_t> api_block_info_;
  std::unordered_map<std::string, int32_t> api_req_info_;
  LbankRateTracker* local_rate_tracker_;
};

}  // namespace coin2::exchange::lbank::api_order
